#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>

using namespace std;

char g[2][4];
unordered_map<string, int> dist;
unordered_map<string, pair<char, string>> pre;
queue<string> q;

void set(string state) {
    for (int i = 0; i < 4; ++ i) g[0][i] = state[i];
    for (int i = 0, j = 7; i < 4; ++ i, -- j) g[1][i] = state[j];
}

string get() {
    string state;
    for (int i = 0; i < 4; ++ i) state += g[0][i];
    for (int i = 3; i >= 0; -- i) state += g[1][i];
    return state;
}

string op0(string state) {
    set(state);
    for (int i = 0; i < 4; ++ i) swap(g[0][i], g[1][i]);
    return get();
}

string op1(string state) {
    set(state);
    char last1 = g[0][3], last2 = g[1][3];
    
    for (int i = 3; i > 0; -- i) {
        g[0][i] = g[0][i - 1];
        g[1][i] = g[1][i - 1];
    }
    g[0][0] = last1, g[1][0] = last2;
    return get();
}

string op2(string state) {
    set(state);
    char cc = g[1][2];
    g[1][2] = g[0][2];
    g[0][2] = g[0][1];
    g[0][1] = g[1][1];
    g[1][1] = cc;
    return get();
}

void bfs(string start, string end) {
    if (start == end) return ;
    
    q.push(start);
    dist[start] = 0;
    
    while (q.size()) {
        auto t = q.front();
        q.pop();
        
        string str[3];
        str[0] = op0(t); 
        str[1] = op1(t);
        str[2] = op2(t);
        
        for (int i = 0; i < 3; ++ i) {
            string s = str[i];
            if (!dist.count(s)) {
                dist[s] = dist[t] + 1;
                pre[s] = {(i + 'A'), t};
                if (s == end) return ;
                q.push(s);
            }
        }
    }
}

int main() {
    string start = "12345678", end;
    for (int i = 0; i < 8; ++ i) {
        int x;
        cin >> x;
        end += (x + '0');
    }
    
    bfs(start, end);
    cout << dist[end] << endl;
    
    string op;
    while (end != start) {
        op += pre[end].first;
        end = pre[end].second;
    }
    reverse(op.begin(), op.end());
    if (op.size()) cout << op << endl;
    
    return 0;
}